package learn.advance.outputstreamwriter1;
// OutputStreamWriter: 是字符流通向字节流的桥梁：
// 可使用指定的 charset 将要写入流中的字符编码成字节。
// (编码:把能看懂的变成看不懂)

import java.io.*;

class Demo2OutputStreamWriter {
    public static void Test() throws IOException {
//        WriteGBK();
//        WriteUTF8();
       // ReadGBK();
        ReadUTF8();
    }

    private static void WriteGBK() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream("D:\\workplace\\新建文件夹\\test\\a.txt"), "GBK");

        osw.write("你好");
        osw.flush();
        osw.close();
    }

    private static void WriteUTF8() throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream("D:\\workplace\\新建文件夹\\test\\a.txt"));

        osw.write("你好");
        osw.flush();
        osw.close();
    }

    private static void ReadGBK() throws IOException {
        InputStreamReader isr=new InputStreamReader(
                new FileInputStream("D:\\workplace\\新建文件夹\\test\\a.txt"),"GBK");

        int len=0;
        while ((len= isr.read())!=-1){
            System.out.println((char)len);
        }
    }

    private static void ReadUTF8() throws IOException {

        InputStreamReader isr=new InputStreamReader(
                new FileInputStream("D:\\workplace\\新建文件夹\\test\\a.txt"));

        int len=0;
        while ((len= isr.read())!=-1){
            System.out.println((char)len);
        }
    }
}

public class TestOutputStreamWriter {
    public static void main(String[] args) throws IOException {
        Demo2OutputStreamWriter.Test();
    }

}
